package com.zhuozhengsoft.Samples5.controller;

import com.zhuozhengsoft.pageoffice.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("SaveAsPDF")
public class SaveAsPDFController {

    @RequestMapping("/WordToPDF")
    public ModelAndView openWord(HttpServletRequest request, Map<String, Object> map) throws Exception{
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //设置服务器页面
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save()", 1);
        poCtrl.addCustomToolButton("另存为PDF文件", "SaveAsPDF()", 1);
        //设置保存页面
        poCtrl.setSaveFilePage("save");//设置处理文件保存的请求方法
        String fileName = "template.doc";
        String pdfName = fileName.substring(0, fileName.length() - 4) + ".pdf";
        //打开word
        poCtrl.webOpen("/doc/SaveAsPDF/"+ fileName, OpenModeType.docNormalEdit, "李斯");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        map.put("pdfName",pdfName);
        ModelAndView mv = new ModelAndView("SaveAsPDF/WordToPDF");
        return mv;
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(request.getSession().getServletContext().getRealPath("doc/SaveAsPDF/")+fs.getFileName());
        fs.close();
    }

    @RequestMapping("/OpenPDF")
    public ModelAndView openPDF(HttpServletRequest request, Map<String, Object> map) throws Exception{
        // 按键说明：光标键、Home、End、PageUp、PageDown可用来移动或翻页；数字键盘+、-用来放大缩小；数字键盘/、*用来旋转页面。
        PDFCtrl pdfCtrl = new PDFCtrl(request);//定义PDFCtrl控件对象
        pdfCtrl.setServerPage(request.getContextPath() + "/poserver.zz"); //设置服务器页面
        pdfCtrl.setTheme(ThemeType.CustomStyle);//设置主题样式

        //添加自定义按钮
        pdfCtrl.addCustomToolButton("打印", "Print()", 6);
        pdfCtrl.addCustomToolButton("-", "", 0);
        pdfCtrl.addCustomToolButton("实际大小", "SetPageReal()", 16);
        pdfCtrl.addCustomToolButton("适合页面", "SetPageFit()", 17);
        pdfCtrl.addCustomToolButton("适合宽度", "SetPageWidth()", 18);
        pdfCtrl.addCustomToolButton("-", "", 0);
        pdfCtrl.addCustomToolButton("首页", "FirstPage()", 8);
        pdfCtrl.addCustomToolButton("上一页", "PreviousPage()", 9);
        pdfCtrl.addCustomToolButton("下一页", "NextPage()", 10);
        pdfCtrl.addCustomToolButton("尾页", "LastPage()", 11);
        pdfCtrl.addCustomToolButton("-", "", 0);
        pdfCtrl.addCustomToolButton("左转", "RotateLeft()", 12);
        pdfCtrl.addCustomToolButton("右转", "RotateRight()", 13);
        pdfCtrl.addCustomToolButton("-", "", 0);
        pdfCtrl.addCustomToolButton("放大", "ZoomIn()", 14);
        pdfCtrl.addCustomToolButton("缩小", "ZoomOut()", 15);
        pdfCtrl.addCustomToolButton("-", "", 0);
        pdfCtrl.addCustomToolButton("全屏", "SwitchFullScreen()", 4);
        //设置禁止拷贝
        pdfCtrl.setAllowCopy(false);
        String fileName = request.getParameter("fileName");//定义文件名称
        pdfCtrl.webOpen("/doc/SaveAsPDF/"+ fileName);//打开文件

        map.put("pageoffice", pdfCtrl.getHtmlCode("PageOfficeCtrl1"));
        ModelAndView mv = new ModelAndView("SaveAsPDF/OpenPDF");
        return mv;
    }
}